home *** CD-ROM | disk | FTP | other *** search
-
- ========================================================================
- Just4Fun Productions presents:
-
- Part 3 of: the Techtutor
- (Topics never covered)
-
-
- HighScores
- ========================================================================
-
- Introduction
- ============-
- Welcome to part 3 of the Techtutor, this one is mainly for SuperFX users,
- because it explains how to read a string using the keyboard. It does however
- contains some information for beginners interested in putting a great
- highscore table in their game...
- For an example of a very stylish high-score, read TECH03.PAS
-
- ----------------------------------------------------------------
- For contacting Just4Fun or me (PeeBee) use the following ways:
-
- Internet : http://people.zeelandnet.nl/rpb
- Email : just4fun@zeelandnet.nl
-
- SnailMail: P.Bestebroer
- Anthony Edenlaan 28
- 4463 JC Goes (Zld.)
- Holland
-
- ICQ : 2309110 (probably fastest way to contact me)
- ----------------------------------------------------------------
-
- Need more input...
- ==================-
-
- First of all we'll need to get the name of the player, and the best way
- would be to let him/her type it. One problem occurs when using the engine,
- because we don't have the normal keyboard interupt...
- We could ofcourse switch back to the old one, but it can be done different.
- The Engine contains an array wich has the "names" of all the keys, and
- we are going to use that array... For the example, take a look at the
- GETINPUT procedure (tech03.pas) This procedure will get the input of the
- player, and return it. Note that this procedure could still use some work,
- like checking on ESCAPE to exit the input, and it has a few bugs in it. But
- the most important thing is shown, the checking on the correct key-pressed.
-
- HighScore typecasting
- =====================-
-
- Before we code anything, we'll have to define a HIGHSCORE type like this:
-
- TYPE TScore = record
- name : string[8];
- score : longint;
- level : word;
- date : Longint;
- end;
-
- VAR HiScore : array[1..15] of Tscore;
-
-
- The example will record the following things about the new high-score:
- * Name of player, the most important item
- * Score of player, very nice to know...
- * Level reached, the level the player reached.
- * Date of game, this is to show when the game was played...
- Ofcourse not all these are needed, but just use what you want in you'r
- game, and simply erase the rest. Now that we have the type, we made an array
- of 1..15, so we can record 15 players..again this can be changed for you'r
- own desires.
-
- Who makes the list...
- =====================-
-
- The next thing we should do is checking if the player has a new highscore.
- To do this, we'll have to walk thru the existing list, and see if the
- score is larger then the lowest...if it is we need to know if it's higher
- then the next, and the next, and the next, etc...
-
- {=-=-= Example =-=-=-=}
- FUNCTION SeeHIScore(Nscore:longint;Nlevel:word):boolean;
- VAR i,j : byte;
- done : boolean;
- dow,hs : word;
- tempD : DateTime;
-
- BEGIN
- SeeHiscore:=false;
- if Nscore<HiScore[15].score then exit;{ if score is lower than last
- one,exit}
- i:=15;
- done:=false;
- repeat
- if Nscore>HiScore[i].score then dec(i) else Done:=true;
- until (i=0) or (done);
- inc(i);
- SeeHiScore:=true;
- for j:=14 downto i do HiScore[j+1]:=HiScore[j];
- with HiScore[i] do begin { add new highscore }
- Name:=GetName; { get the name }
-
- score := Nscore; { add score }
- level := NLevel; { add level }
- GetDate(TempD.year,TempD.month,TempD.day,dow);
- GetTime(TempD.hour,TempD.min,TempD.sec,hs);
- PackTime(TempD,date); { add date }
- end;
- END;
- {=-=-= End of Example =-=-=-=}
-
- This procedure will not only look if we got an highscore, it will also
- add the new score to the list, and call the GETINPUT procedure for the name.
-
- Show me, I wanna see it
- =======================-
-
- So now we got a high-score table...but we still can't see it. Showing the
- table is the most simple thing in the SuperFX Engine, just walk thru the
- table, and write the text on screen! You could, ofcourse, first draw a
- background or something and then put the text "over" it. For the example
- look at SHOWHISCORE. It will display the complete table (NOTE: this
- procedure is different from the one found in the example, this one can be
- used without the SuperFX engine).
-
-
- {=-=-=-= Example =-=-=-=}
- PROCEDURE ShowHIScore; { for normal text-screens untested!!! }
- VAR i : byte;
- zin : string;
- Tdate: DateTime;
- ScoreOFS : byte;
- BEGIN
- textcolor(7); textbackground(0); clrscr;
- ScoreOfs:=2;
-
- REPEAT
- gotoxy(1,1); write('NR');
- gotoxy(4,1,); write('Name');
- gotoxy(14,1); write('Score');
- gotoxy(20,1); write('Level');
- gotoxy(26,1); write('Time');
- gotoxy(36,1); write('Date');
-
- for i:=1 to 15 do begin
- with HiScore[i] do begin
- {
- Show NAME and rank
- }
- str(i,zin);
- while length(zin)<2 do zin:=' '+zin;
- zin:=zin+'. '+NAME;
- gotoxy(1,scoreofs+i); write(zin);
- {
- Show the Score
- }
- str(score,zin);
- while length(zin)<9 do zin:='.'+zin;
- gotoxy(4,scoreofs+i); write(zin);
- {
- Show the level
- }
- str(level,zin);
- gotoxy(20,scoreofs+i); write(zin);
- {
- Get the time of the played game
- }
- UnpackTime(Date,TDate);
- {
- Show the hour played
- }
- str(Tdate.hour,zin);
- while length(zin)<2 do zin:=' '+zin;
- zin:=zin+':';
- gotoxy(26,scoreofs+i); write(zin);
- {
- Show minutes
- }
- str(Tdate.Min,zin);
- while length(zin)<2 do zin:='0'+zin;
- gotoxy(29,scoreofs+i); write(zin);
- {
- Show month name
- }
- zin:=MNames[Tdate.Month];
- zin:=zin+'-';
- gotoxy(36,scoreofs+i); write(zin);
- {
- Show the day
- }
- str(Tdate.day,zin);
- while length(zin)<2 do zin:=' '+zin;
- gotoxy(40,scoreofs+i); write(zin);
- {
- Show the Year
- }
- str(Tdate.year,zin);
- gotoxy(43,scoreofs+i); write(zin);
- end;
- end;
-
- repeat until port[$60]<>156;
- END;
-
- {=-=-=-= End of Example =-=-=-=}
-
- Loading the score
- =================-
-
- A highscore should ofcourse be saved to disk, so it can be loaded later
- when the game is played again... so that's what we are going to do.
- First of all we should be able to load the existing High-score table.
- You could start loading the file, but what if the file does not exist?
- so at the start of you'r game make sure you "fill" the high-score with
- some fake values, but remember that the player has to be able to easily
- get higher scores then the fake ones! Now we have to check if the file
- exists, if it does we can safely start loading. Note that you don't go and
- read in a loop from 1 to 15 (or 10), because maybe some one tinkered with
- the scores and there might only be 4 highscores in the file. So keep track
- of that..for an example look at the LOADSCORES procedure.
- Note: You could add a nice thing here, because if you find somethings wrong
- with the highscore file, you could create a new blank table, and put in very
- hi scores the list...just a thought.
-
- {=-=-=-= Example =-=-=-=}
- PROCEDURE LoadScores;
- VAR f : file;
- i : byte;
- dow,hs : word;
- tempD : DateTime;
- BEGIN
- assign(f,'hiscore.his'); { name of the hiscore file }
- {$I-}
- reset(f,1);
- {$I+}
- if ioresult<>0 then begin { No hiscore file found, so create one! }
- rewrite(f,1);
- Hiscore[01].score:=1500; Hiscore[02].score:=1400;
- Hiscore[03].score:=1300; Hiscore[04].score:=1200;
- Hiscore[05].score:=1100; Hiscore[06].score:=1000;
- Hiscore[07].score:=0900; Hiscore[08].score:=0800;
- Hiscore[09].score:=0700; Hiscore[10].score:=0600;
- Hiscore[11].score:=0500; Hiscore[12].score:=0400;
- Hiscore[13].score:=0300; Hiscore[14].score:=0200;
- Hiscore[15].score:=0100;
- for i:=1 to 15 do begin
- with HiScore[i] do begin
- Name:='ME!';
- Level:=$101;
- GetDate(TempD.year,TempD.month,TempD.day,dow);
- GetTime(TempD.hour,TempD.min,TempD.sec,hs);
- PackTime(TempD,date);
- end;
- blockwrite(f,hiscore[i],sizeof(TScore));
- end;
- close(f);
- exit; { done the loading, so exit }
- end;
- i:=1;
- while (not eof(f)) and (i<16) do begin
- blockread(f,hiscore[i],sizeof(TSCORE));
- inc(i);
- end;
- close(f);
- END;
- {=-=-= End of Example =-=-=}
-
-
- Saving the score
- ================-
-
- After the player get's a new high-score, it's best to save it at once.
- because if the game crashes the player might loose it's score, and be
- pissed off at the game, and the programmer (you!). The saving is the easiest
- of all procedures:
- * Open a NEW file
- * Loop thru the scores, saving them one by one...
- I know that you should do some error checking, because if the game is
- run from CD, you won't be able to save!
-
- {=-=-= Example =-=-=}
- PROCEDURE SaveScores;
- VAR f : file;
- i : byte;
- dow,hs : word;
- tempD : DateTime;
- BEGIN
- assign(f,'hiscore.his');
- rewrite(f,1);
- for i:=1 to 15 do blockwrite(f,hiscore[i],sizeof(TSCORE));
- close(f);
- END;
- {=-=-= End of Example =-=-=-=}
-
-
- That's all folks
- ================-
-
- Well that was a brief explanation about high-scores, and how to
- implement them. I know that most people won't even look at this because
- they think it's to simple for them, well they had to learn it somewhere
- and might not even be doing it that correctly, who knows? But for the
- beginning game-programmers this could be a usefull text, and for the SuperFX
- Engine users it handy to know who to read the keyboard keys...
-
- Next Tech topic: Collisions
-
-
- PeeBee - September 10th '97
-